function MYstruct2Node(s, varargin)
    p = inputParser;
    p.addRequired('s', @(x) isscalar(x) && isstruct(x));
    p.addRequired('container', @(x) isa(x, 'matlab.ui.Figure')|| isa(x, 'matlab.ui.container.Panel'));
    p.parse(s, varargin{:});


    tree = make_UIs(p.Results.container);
    buildtree(tree, s, 'Root');
end

%%
function buildtree(tree, s, pathStr)
    nodeData.class = class(s);
    nodeData.path = pathStr;
    rootnode = uitreenode(tree, 'Text', pathStr, 'NodeData', nodeData);
    
    cellfun(@(name) buildNode(rootnode, s, name), fieldnames(s));
end

function buildNode(parentNode, s, name)
    if ~isscalar(s)
        arrayfun(@(x) buildNode(parentNode, x, name), s);
        return;
    end

    val = s.(name);
    nodeNameStr = sprintf('%s [%s x %d]', name, class(val), length(val));
    
    if iscell(val)
        pathStr = sprintf('%s.%s', parentNode.NodeData.path, name);
        nodeData.class = class(val);
        nodeData.path = pathStr;
        
        childNode = uitreenode(parentNode, 'Text', nodeNameStr, 'NodeData', nodeData);
        
        for k = 1:length(val)
            nodeData_k.class = class(val{k});
            nodeData_k.path = sprintf('%s{%d}', pathStr, k);
            nodeText_k = sprintf('%s{%d} [%s]', name, k, class(val{k}));
            
            uitreenode(childNode, 'Text', nodeText_k, 'NodeData', nodeData_k );
        end
    else
        pathStr = sprintf('%s.%s', parentNode.NodeData.path, name);
        nodeData.class = class(val);
        nodeData.path = pathStr;
        childNode = uitreenode(parentNode, 'Text', nodeNameStr, 'NodeData', nodeData);
    end

    [isLeaf, names] = leafTest(val);
    if ~isLeaf
        cellfun(@(x) buildNode(childNode, val, x), names);
    end
end

function [isLeaf, names] = leafTest(s)
    try
        names = fieldnames(s);
        isLeaf = false;
    catch
        names = [];
        isLeaf = true;
    end
end

%%
function tree = make_UIs(container)
    pos = container.Position; cWidth = pos(3); cHeight = pos(4);
    
    leftMargin = 10; rightMargin = 10; topMargin = 50; bottomMargin = 10;
    buttonWidth = 60; buttonHeight = 22; horizGap = 20; vertGap = 15;
    
    tree = uitree(container, 'Position', [leftMargin, bottomMargin+buttonHeight+vertGap, cWidth-leftMargin-rightMargin, cHeight-bottomMargin-vertGap-topMargin]);
    b1 = uibutton(container, 'Position', [leftMargin, bottomMargin, buttonWidth, buttonHeight], 'Text', 'Display', 'ButtonPushedFcn', @(btn, event) BtnDispPushed(btn, event, tree) );
    b2 = uibutton(container, 'Position', [leftMargin+buttonWidth+horizGap, bottomMargin, buttonWidth, buttonHeight], 'Text', 'Backward', 'ButtonPushedFcn', @(btn, event) BtnBackwardPushed(btn, event, tree) );
    b3 = uibutton(container, 'Position', [leftMargin+2*buttonWidth+2*horizGap, bottomMargin, buttonWidth, buttonHeight], 'Text', 'Forward', 'ButtonPushedFcn', @(btn, event) BtnForwardPushed(btn, event, tree) );
end

function BtnDispPushed(btn, event, tree)
    disp(tree.SelectedNodes);
    disp('Button Disp Pushed.');
end

function BtnBackwardPushed(btn, event, tree)
    disp(tree.SelectedNodes);
    disp('Button Backward Pushed.');
end

function BtnForwardPushed(btn, event, tree)
    disp(tree.SelectedNodes);
    disp('Button forward Pushed.');
end